package org.spring.springboot.controller;

import javax.servlet.http.HttpServletRequest;

import org.spring.springboot.entity.User;
import org.spring.springboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 60055558 项目名称：springboot 类名称：UserController 创建人：60055558
 *         创建时间：2020年7月17日 下午4:09:01
 * @version
 */

//@CrossOrigin(origins = "*",maxAge = 3600)
@RestController
@RequestMapping("/testBoot")
public class UserController {

	@Autowired
	private UserService userService;

	// 查询
	@GetMapping("/getUser")
	@ResponseBody
	public String GetUsers(@RequestParam(value="id") int id ) {
		return userService.Sel(id).toString();
	}
	
	// 查询
	@RequestMapping("/getUser/{id}")
	@ResponseBody
	public String GetUser(@PathVariable int id) {
		return userService.Sel(id).toString();
	}

	// 更新
	@RequestMapping("/updateUser")
	@ResponseBody
	public String updateUser(HttpServletRequest request) {
		String realName = request.getParameter("realName");
		int id = Integer.parseInt(request.getParameter("id"));
		User user = new User();
		user.setRealName(realName);
		user.setId(id);
		if (userService.updateUserById(user)) {
			return "更新成功！";
		} else {
			return "更新失败！";
		}
	}

	// 新增
	@RequestMapping("/insertUser")
	@ResponseBody
	public String insertUser(HttpServletRequest request) {
		/*
		int id = Integer.parseInt(request.getParameter("id"));
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String realName = request.getParameter("realName");
		User user = new User();
		user.setId(id);
		user.setUserName(userName);
		user.setPassWord(passWord);
		user.setRealName(realName);
		*/
		if (userService.insertUser()) {
			return "插入成功！";
		} else {
			return "插入失败！";
		}
	}

	// 删除
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(int id) {
		if (userService.deleteById(id)) {
			return "删除成功！";
		} else {
			return "删除失败！";
		}
	}

}
